package hnlg.com.cn.purviewmanage.rolemanage.control;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import hnlg.com.cn.common.RespBean;
import hnlg.com.cn.model.ModuleVO;
import hnlg.com.cn.model.RoleVO;
import hnlg.com.cn.purviewmanage.modulemanage.service.IModuleService;
import hnlg.com.cn.purviewmanage.rolemanage.service.IRoleService;

@RestController
@RequestMapping("/RoleManageControl")
public class RoleManageControl {
List<RoleVO> list=null;
RespBean rb=null;
RoleVO vo=null;
@Resource IRoleService RoleService;
@Resource IModuleService ModuleService;
//http://localhost:8044/test/RoleManageControl/queryList?
@RequestMapping("/queryList")
public List<RoleVO> queryList(
	@RequestParam("rolename")String rolename){
	try{
		RoleVO vo=new RoleVO();
		vo.setRolename(rolename);
		list=RoleService.queryList(vo);
	}catch(Exception e){
		e.printStackTrace();
	}
	return list;
}
	
@RequestMapping("/addRole")
public RespBean addRole(RoleVO vo){
	try{
	RoleService.addRole(vo);
	rb=new RespBean("msg", "添加成功");
	}catch(Exception e){
		rb=new RespBean("msg", "添加失败");
		e.printStackTrace();
	}
	return rb;
}

@RequestMapping("/updateRole")
public RespBean updateRole(RoleVO vo){
	try{
		System.out.println("roleid="+vo.getRoleid());
		boolean flag=RoleService.updateRole(vo);
		System.out.println(flag);
		rb=new RespBean("msg", "更新成功");
	}
		catch(Exception e){
			rb=new RespBean("msg", "更新失败");
			e.printStackTrace();
		}
		return rb;
	}

@RequestMapping("/deleteRole")
public RespBean deleteRole(
		@RequestParam("roleid")String roleid){
	try{
	System.out.println("roleid="+roleid);	
	RoleService.deleteRole(roleid);
	rb=new RespBean("msg", "删除成功");
	}catch(Exception e){
		rb=new RespBean("msg", "删除失败");	
		e.printStackTrace();
	}
	return rb;
}

/* 去授权页面的处理：模块显示列表数据 */
@RequestMapping("/getModuleList")
public List<ModuleVO> getModuleList(
  @RequestParam(required=false,defaultValue="")String roleid)throws Exception{
	List<ModuleVO> list=null;
	try{
	list=ModuleService.queryList(new ModuleVO());
		
	}catch(Exception e){
		e.printStackTrace();
	}
	return list;
}

//角色授权模块
@RequestMapping("/RoleaddModule")
public RespBean RoleaddModule(
	@RequestParam(required=false,defaultValue="") String roleid,
	@RequestParam(required=false,defaultValue="")String modulecheckbox[]){
	try{
	/*这里要做特需的处理，当授权选中了checkbox时按下面处理：先删除再加入
	 * 如果没有选中checkbox：代表删除权限，此时的处理应该只有删除角色授权的模块，不需要增加
	 * 没有值去增加，mybatis会报错
	 */
		if(null!=modulecheckbox&&modulecheckbox.length>0){
	    this.RoleService.roleaddModule(roleid, modulecheckbox);
		}
		else{
			this.ModuleService.deleteModule(roleid);
		}
		rb=new RespBean("msg", "授权成功");
	}catch(Exception e){
		rb=new RespBean("msg", "授权失败");
		e.printStackTrace();
	}
	return rb;
}

/*查询那些模块已经授了权的模块id*/
@RequestMapping("/getExistModuleId")
public List<String> getExistModuleId(
		@RequestParam(required=false,defaultValue="")String roleid)throws Exception{
	List<String> modulelist=null;
	try{
		/*根据角色id查询中间表：role_module表，该角色已经授权的模块id列表*/
		modulelist=this.RoleService.queryModuleIdList(roleid);
	}catch(Exception e){
		e.printStackTrace();
	}
	return modulelist;
}

}
